
// DrawDotMatrixView.cpp : implementation of the CDrawDotMatrixView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "DrawDotMatrix.h"
#endif

#include "DrawDotMatrixDoc.h"
#include "DrawDotMatrixView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDrawDotMatrixView

IMPLEMENT_DYNCREATE(CDrawDotMatrixView, CScrollView)

BEGIN_MESSAGE_MAP(CDrawDotMatrixView, CScrollView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CDrawDotMatrixView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

// CDrawDotMatrixView construction/destruction

CDrawDotMatrixView::CDrawDotMatrixView()
{
	// TODO: add construction code here
	m_pImg = NULL;

	DWORD bitmapInfoSize,i;

	// Bitmap info structure for the image
	bitmapInfoSize = sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD);
	m_pBitImage = (BITMAPINFO*)new BYTE[bitmapInfoSize];

	m_pBitImage->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	m_pBitImage->bmiHeader.biPlanes = 1;
	m_pBitImage->bmiHeader.biBitCount = 8;
	m_pBitImage->bmiHeader.biCompression = BI_RGB;
	m_pBitImage->bmiHeader.biSizeImage = 0;
	m_pBitImage->bmiHeader.biXPelsPerMeter = 0;
	m_pBitImage->bmiHeader.biYPelsPerMeter = 0;
	m_pBitImage->bmiHeader.biClrUsed = 0;
	m_pBitImage->bmiHeader.biClrImportant = 0;

	m_pBitImage->bmiHeader.biWidth = 0;
	m_pBitImage->bmiHeader.biHeight = 0;

	for (i = 0 ; i < 256 ; i++) 
	{
		m_pBitImage->bmiColors[i].rgbBlue = (BYTE)i;
		m_pBitImage->bmiColors[i].rgbGreen = (BYTE)i;
		m_pBitImage->bmiColors[i].rgbRed = (BYTE)i;
		m_pBitImage->bmiColors[i].rgbReserved = 0;
	}
}

CDrawDotMatrixView::~CDrawDotMatrixView()
{
	delete m_pImg;
	m_pImg = NULL;
}

BOOL CDrawDotMatrixView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

// CDrawDotMatrixView drawing

void CDrawDotMatrixView::OnDraw(CDC* pDC)
{
	CDrawDotMatrixDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	m_pBitImage->bmiHeader.biWidth = pDoc->m_nWidth;
	m_pBitImage->bmiHeader.biHeight = pDoc->m_nHeight;
	SetDIBitsToDevice(pDC->GetSafeHdc(), 0, 0, pDoc->m_nWidth, pDoc->m_nHeight,
										 0, 0, 0, pDoc->m_nHeight,
										 m_pImg, m_pBitImage, DIB_RGB_COLORS);
	
}


// CDrawDotMatrixView printing


void CDrawDotMatrixView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CDrawDotMatrixView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDrawDotMatrixView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDrawDotMatrixView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CDrawDotMatrixView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CDrawDotMatrixView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CDrawDotMatrixView diagnostics

#ifdef _DEBUG
void CDrawDotMatrixView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CDrawDotMatrixView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CDrawDotMatrixDoc* CDrawDotMatrixView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDotMatrixDoc)));
	return (CDrawDotMatrixDoc*)m_pDocument;
}
#endif //_DEBUG


// CDrawDotMatrixView message handlers


void CDrawDotMatrixView::OnInitialUpdate()
{
	CDrawDotMatrixDoc* pDoc = GetDocument();
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	sizeTotal.cx = pDoc->m_nWidth;
	sizeTotal.cy = pDoc->m_nHeight;

	m_pImg = pDoc->m_pImg;
	SetScrollSizes(MM_TEXT, sizeTotal);

	// TODO: Add your specialized code here and/or call the base class
}
